package com.kylin.androidlearn1.acticity.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.kylin.androidlearn1.R;
import com.kylin.androidlearn1.acticity.bean.MyEntity;

import java.util.ArrayList;

/**
 * Created by kylinhuang on 10/3/16.
 */
public class ListViewAdapter  extends BaseAdapter{

    private final Context mContext;
    private ArrayList<MyEntity> data = new ArrayList<MyEntity>();

    public ListViewAdapter(Context mContext) {
        this.mContext = mContext ;
    }

    public ListViewAdapter(Context mContext, ArrayList list) {
        this.mContext = mContext ;
        data = list;
    }

    /** listView 显示 的数目 */
    @Override
    public int getCount() {
        if (null == data || data.size()== 0 ) return 0;
        return data.size();
    }

    /** 返回listView 对象 */
    @Override
    public Object getItem(int position) {
        if ( null == data || null == data.get(position)  )return  null ;
        return data.get(position);
    }

    /** 返回listView item id */
    @Override
    public long getItemId(int position) {
        return position;
    }

    /** 返回listView item  */
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = LayoutInflater.from(mContext);
        View view = inflater.inflate(R.layout.item, null);

        TextView mTextView =  (TextView)view.findViewById(R.id.item_text);
        MyEntity entity =  data.get(position);
        mTextView.setText(entity.getName());

        return view;
    }

    public void setData(ArrayList list) {
        data = list;
        notifyDataSetChanged();
    }
}
